/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gestion;

import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import otros.Fecha;
import otros.MiTableModel;
import otros.Util;
import tablasSql.Clientes;
import tablasSql.ConsultaBD;
import tablasSql.FacturasAlbaranes;

/**
 *
 * @author FERRETERIA
 */

public class FArtXCliente extends javax.swing.JInternalFrame {    
    boolean fuera;
    int filaSel;
    private final static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(FArtXCliente.class);
     
    public FArtXCliente() {
        Util.setConfiguracionLog(Util.LOG4_FICH);
        initComponents();
        AnchoColumnas();
       
        tabla.setColumnSelectionAllowed(false);
        tabla.setAutoCreateRowSorter(true);        
        String []fecha = new Fecha().getFecha().split("-");
        //txtDia.setText(fecha[0]);
        //txtMes.setText(fecha[1]);
        txtAño.setText(fecha[2]);
        
        bBuscar.setEnabled(false);
    }
    
    private void AnchoColumnas() {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel)tabla.getColumnModel();
        colModel.getColumn(1).setPreferredWidth(350);
    }
    
    private void ActualizarTabla() { 
        ConsultaBD.cursorEspera(this);
        int dia = Integer.parseInt(txtDia.getText().isEmpty()? "0" : txtDia.getText());
        int mes = Integer.parseInt(txtMes.getText().isEmpty()? "0" : txtMes.getText());
        int año = Integer.parseInt(txtAño.getText().isEmpty()? "0" : txtAño.getText());
        String idArticulo = txtCodigo.getText().isEmpty()? "0" : txtCodigo.getText();
        int tam = tabla.getRowCount();
        for (int i = 0; i < tam; i++)
            ((DefaultTableModel)tabla.getModel()).removeRow(0); 
        
        MiTableModel datos = FacturasAlbaranes.getTablaArtXCliente(idArticulo, dia, mes, año);
        tabla.setModel(datos);
        datos.getRowCount();
        
        tabla.getColumnModel().getColumn(0).setCellRenderer(new tableCellRender.Id(filaSel, fuera));
        tabla.getColumnModel().getColumn(1).setCellRenderer(new tableCellRender.Texto(filaSel, fuera));
        tabla.getColumnModel().getColumn(2).setCellRenderer(new tableCellRender.Stock(filaSel, fuera));
        tabla.getColumnModel().getColumn(3).setCellRenderer(new tableCellRender.Texto(filaSel, fuera));
        tabla.getColumnModel().getColumn(4).setCellRenderer(new tableCellRender.Fecha(filaSel, fuera));
            
        AnchoColumnas();
        txtNumReg.setText(""+tabla.getRowCount());
        
       
        ConsultaBD.cursorNormal(this);
    }
 
    private void abrirFactura(String id, int tipo) {
        FTPV tpv = new FTPV();
        this.getParent().add(tpv);
        Principal.centrarFrame(tpv);
        tpv.show();    
        tpv.cargar(id, tipo);
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel4 = new javax.swing.JLabel();
        txtNumReg = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtDia = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        txtMes = new javax.swing.JFormattedTextField();
        bBuscar = new javax.swing.JButton();
        txtAño = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JFormattedTextField();
        jLabel11 = new javax.swing.JLabel();
        txtNombreCli = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Articulo comprados por cliente");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/img/icono2.png"))); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabel4.setText("Nº reg");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 22, 35);
        getContentPane().add(jLabel4, gridBagConstraints);

        txtNumReg.setEditable(false);
        txtNumReg.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNumReg.setMinimumSize(new java.awt.Dimension(80, 20));
        txtNumReg.setPreferredSize(new java.awt.Dimension(80, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        getContentPane().add(txtNumReg, gridBagConstraints);

        jPanel1.setMinimumSize(new java.awt.Dimension(400, 70));
        jPanel1.setPreferredSize(new java.awt.Dimension(400, 70));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Criterios de busqueda:  (SOLO EN FACTURAS)  ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
        jPanel1.add(jLabel1, gridBagConstraints);

        jLabel2.setText("Dia");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 0, 32);
        jPanel1.add(jLabel2, gridBagConstraints);

        txtDia.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("00"))));
        txtDia.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDia.setMinimumSize(new java.awt.Dimension(25, 20));
        txtDia.setPreferredSize(new java.awt.Dimension(25, 20));
        txtDia.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtDiaMouseClicked(evt);
            }
        });
        txtDia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDiaKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        jPanel1.add(txtDia, gridBagConstraints);

        jLabel3.setText("Mes");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel1.add(jLabel3, gridBagConstraints);

        txtMes.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("00"))));
        txtMes.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtMes.setMinimumSize(new java.awt.Dimension(25, 20));
        txtMes.setPreferredSize(new java.awt.Dimension(25, 20));
        txtMes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtMesMouseClicked(evt);
            }
        });
        txtMes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtMesKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 25, 0, 0);
        jPanel1.add(txtMes, gridBagConstraints);

        bBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar.png"))); // NOI18N
        bBuscar.setMinimumSize(new java.awt.Dimension(61, 61));
        bBuscar.setPreferredSize(new java.awt.Dimension(61, 61));
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(bBuscar, gridBagConstraints);

        txtAño.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("0000"))));
        txtAño.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtAño.setToolTipText("yyyy");
        txtAño.setMinimumSize(new java.awt.Dimension(35, 20));
        txtAño.setPreferredSize(new java.awt.Dimension(35, 20));
        txtAño.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtAñoMouseClicked(evt);
            }
        });
        txtAño.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtAñoKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 25, 0, 0);
        jPanel1.add(txtAño, gridBagConstraints);

        jLabel5.setText("Año");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 0, 0);
        jPanel1.add(jLabel5, gridBagConstraints);

        jLabel7.setText("Código cliente");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 0, 0);
        jPanel1.add(jLabel7, gridBagConstraints);

        txtCodigo.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        txtCodigo.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCodigo.setToolTipText("");
        txtCodigo.setMinimumSize(new java.awt.Dimension(50, 20));
        txtCodigo.setPreferredSize(new java.awt.Dimension(50, 20));
        txtCodigo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtCodigoMouseClicked(evt);
            }
        });
        txtCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCodigoKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 0, 0);
        jPanel1.add(txtCodigo, gridBagConstraints);

        jLabel11.setText("Fecha comienzo de busqueda:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 3);
        jPanel1.add(jLabel11, gridBagConstraints);

        txtNombreCli.setEditable(false);
        txtNombreCli.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNombreCli.setMinimumSize(new java.awt.Dimension(180, 20));
        txtNombreCli.setPreferredSize(new java.awt.Dimension(180, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel1.add(txtNombreCli, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 175;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 10, 0, 20);
        getContentPane().add(jPanel1, gridBagConstraints);

        jScrollPane1.setMinimumSize(new java.awt.Dimension(400, 325));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(400, 325));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Cliente", "Total", "Fecha", ""
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla.setToolTipText("Boton dch raton abre la factura");
        tabla.setColumnSelectionAllowed(true);
        tabla.getTableHeader().setReorderingAllowed(false);
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                tablaMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                tablaMouseExited(evt);
            }
        });
        tabla.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                tablaMouseMoved(evt);
            }
        });
        jScrollPane1.setViewportView(tabla);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        ActualizarTabla();
    }//GEN-LAST:event_bBuscarActionPerformed

    private void txtDiaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtDiaMouseClicked
        txtDia.selectAll();
    }//GEN-LAST:event_txtDiaMouseClicked

    private void txtMesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtMesMouseClicked
        txtMes.selectAll();
    }//GEN-LAST:event_txtMesMouseClicked

    private void txtDiaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDiaKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
            txtMes.requestFocus();
        }
    }//GEN-LAST:event_txtDiaKeyReleased

    private void txtMesKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMesKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
        }
    }//GEN-LAST:event_txtMesKeyReleased

    private void txtAñoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAñoKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
        }
    }//GEN-LAST:event_txtAñoKeyReleased

    private void txtAñoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtAñoMouseClicked
        txtAño.selectAll();
    }//GEN-LAST:event_txtAñoMouseClicked

    private void txtCodigoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCodigoMouseClicked
        txtCodigo.selectAll();
    }//GEN-LAST:event_txtCodigoMouseClicked

    private void txtCodigoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoKeyReleased
        switch (evt.getKeyCode()) {
        case 113:       //f2
            FClientes c = new FClientes(this,"art");
            this.getParent().add(c);
            Principal.centrarFrame(c);
            c.show();
            bBuscar.setEnabled(true);
            break;
        case 10:        // Enter 
            txtNombreCli.setText(Clientes.getNomCliente(txtCodigo.getText()));
            bBuscar.setEnabled(true);
            ActualizarTabla();
            break;
        default:
            bBuscar.setEnabled(false);
            break;
        }           
    }//GEN-LAST:event_txtCodigoKeyReleased

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked
        int fila = tabla.rowAtPoint(evt.getPoint());
        int columna = tabla.columnAtPoint(evt.getPoint());
        if(evt.isMetaDown()) {
            abrirFactura(""+tabla.getValueAt(fila, 3),0);
        }

    }//GEN-LAST:event_tablaMouseClicked

    private void tablaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseEntered
        fuera = false;
    }//GEN-LAST:event_tablaMouseEntered

    private void tablaMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseExited
        fuera = true;
    }//GEN-LAST:event_tablaMouseExited

    private void tablaMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseMoved
        JTable aTable =  (JTable)evt.getSource();
        if (!fuera) {
            filaSel = aTable.rowAtPoint(evt.getPoint());
        }
    }//GEN-LAST:event_tablaMouseMoved

         
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBuscar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabla;
    private javax.swing.JFormattedTextField txtAño;
    public javax.swing.JFormattedTextField txtCodigo;
    private javax.swing.JFormattedTextField txtDia;
    private javax.swing.JFormattedTextField txtMes;
    public javax.swing.JTextField txtNombreCli;
    private javax.swing.JTextField txtNumReg;
    // End of variables declaration//GEN-END:variables
}
